import { MagicWeaponBase } from "./MagicWeaponBase";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../resource/ImagesConst";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { ResolveItemRenderer } from "./renderer/ResolveItemRenderer";
import { ObjCreate } from "../common/ObjCreate";
import { ScrollPolicy } from "../../../../flashx/textLayout/container/ScrollPolicy";
import { MWPropertyItemRenderer } from "./renderer/MWPropertyItemRenderer";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { Language } from "../../../../extend/language/Language";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { MagicWeaponItemData } from "./MagicWeaponItemData";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { ShiftVo } from "./data/ShiftVo";
import { Alert } from "../../../../com/mui/controls/Alert";
import { PulseSharedObject } from "../player/PulseSharedObject";
import { EBind } from "../../../../Message/Public/EBind";
import { CheckBoxWin } from "../common/alertwins/CheckBoxWin";
import { MsgManager } from "../../manager/MsgManager";
import { ItemData } from "../../resource/info/ItemData";
import { MagicWeaponUtil } from "./MagicWeaponUtil";
import { Cache } from "../../cache/Cache";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { MagicWeaponConfig } from "../../resource/MagicWeaponConfig";
import { GButton } from "../../../../com/mui/controls/GButton";
import { GTileList } from "../../../../com/mui/controls/GTileList";
type int = number;
//class MagicWeaponTransferPanel
    
    export  class MagicWeaponTransferPanel extends MagicWeaponBase
    {
        constructor()
        {
            super();
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            var loc1=new GImageBitmap(ImagesConst.MagicWeaponBg, 43, 188);
            this.addChild(loc1);
            LoaderManager.instance.load("Equip_BigArrow.png", this.onBigArrowLoaded);
            this._shiftItem = new ResolveItemRenderer(false);
            this._shiftItem.x = 65;
            this._shiftItem.y = 175;
            this._shiftItem.addEventListener(flash.events.MouseEvent.CLICK, this.onPutDownShiftItem);
            this.addChild(this._shiftItem);
            this._targetItem = new ResolveItemRenderer(false);
            this._targetItem.x = 303;
            this._targetItem.y = 175;
            this._targetItem.addEventListener(flash.events.MouseEvent.CLICK, this.onPutDownTargetItem);
            this.addChild(this._targetItem);
            ObjCreate.createBg(30, 255, 140, 145, this);
            ObjCreate.createBg(267, 255, 140, 145, this);
            this._shiftItemList = ObjCreate.createTileList(60, 270, 80, 200, this);
            this._shiftItemList.scrollPolicy = ScrollPolicy.OFF;
            this._shiftItemList.columnWidth = 100;
            this._shiftItemList.rowHeight = 22;
            this._shiftItemList.setStyle("cellRenderer", MWPropertyItemRenderer);
            this._targetItemList = ObjCreate.createTileList(300, 270, 130, 200, this);
            this._targetItemList.scrollPolicy = ScrollPolicy.OFF;
            this._targetItemList.columnWidth = 130;
            this._targetItemList.rowHeight = 22;
            this._targetItemList.setStyle("cellRenderer", MWPropertyItemRenderer);
            var loc2=GlobalClass.getBitmap(ImagesConst.EquipRecast_arrow);
            loc2.x = 196;
            loc2.y = 312;
            this.addChild(loc2);
            this._transferBtn = ObjCreate.createGButton(Language.getString(27670), 177, 415, 82, 26, this, "ButtonRed");
            this._transferBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onTransferHandler);
            ObjCreate.createGTextField(Language.getString(27666), 280, 420, 80, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createTextBox("3000", 312, 420, 70, 20, this, GlobalStyle.textFormatWhite);
            ObjCreate.createBitmap(ImagesConst.Tongqian, 363, 421, this);
            ObjCreate.createBg(31, 452, 380, 2, this, ImagesConst.SplitLine);
            ObjCreate.createFireTip(Language.getString(27696), 30, 455, this, true);
            var loc3=ObjCreate.createImgBtn(ImagesConst.RuleExplain, 350, 460, 60, 23, this, "Button");
            loc3.gbutton.toolTipData = Language.getString(27677);
            loc3.gbutton.addEventListener(flash.events.MouseEvent.CLICK, this.clickHelpHandler);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.clean);
            LoaderHelp.addResCallBack(ResFileConst.magicWeapon, this.onLoadResHandler);
            return;
        }

        /* internal  */clickHelpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponShowPageGuide));
            return;
        }

        /* internal  */onLoadResHandler(): void
        {
            var loc1=GlobalClass.getBitmap(ImagesConst.MoveMagicWeapon);
            ObjCreate.setObjAttri(loc1, 59, 180, -1, -1, this);
            var loc2=GlobalClass.getBitmap(ImagesConst.TargetMagicWeapon);
            ObjCreate.setObjAttri(loc2, 296, 180, -1, -1, this);
            return;
        }

        /* internal  */clean(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (!(loc1 && loc1.isSelect)) 
                {
                    continue;
                }
                loc1.isSelect = false;
            }
            operateMaterialData = [];
            this.onPutDownShiftItem();
            this.onPutDownTargetItem();
            if (arg1 && pageSelecter) 
            {
                restPage();
            }
            return;
        }

        /* internal  */onBigArrowLoaded(arg1: ImageInfo): void
        {
            this._bigArrow = new Object /* flash.display.Bitmap */(arg1.bitmapData);
            this._bigArrow.x = 185;
            this._bigArrow.y = 210;
            this.addChild(this._bigArrow);
            return;
        }

        /* internal  */onPutDownShiftItem(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this._shiftItem.data = new MagicWeaponItemData(null);
            this._shiftItemList.dataProvider = new DataProvider([]);
            this._shiftItemList.drawNow();
            if (arg1) 
            {
                this.setSelect();
            }
            return;
        }

        /* internal  */onPutDownTargetItem(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this._targetItem.data = new MagicWeaponItemData(null);
            this._targetItemList.dataProvider = new DataProvider([]);
            this._targetItemList.drawNow();
            if (arg1) 
            {
                this.setSelect();
            }
            return;
        }

        /* internal  */getUId(arg1: ResolveItemRenderer): string
        {
            var loc1=null;
            if (arg1 && arg1.data) 
            {
                loc1 = arg1.data as MagicWeaponItemData;
                if (loc1 && loc1.itemData) 
                {
                    return loc1.itemData.uid;
                }
            }
            return "";
        }

        /* internal  */setSelect(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (loc1 && loc1.itemData && (loc1.itemData.uid == this.getUId(this._shiftItem) || loc1.itemData.uid == this.getUId(this._targetItem))) 
                {
                    loc1.isSelect = true;
                    continue;
                }
                loc1.isSelect = false;
            }
            _materialList.dataProvider = new DataProvider(_materialData);
            _materialList.drawNow();
            if (pageSelecter) 
            {
                pageSelecter.maxPage = int(Math.ceil(_materialData.length / 6));
                currentPage();
            }
            return;
        }

        /* internal  */onTransferHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var vo: ShiftVo;
            var onBtnClickHandler: Function;
            var isNeedAlert: Boolean;

            var loc1;
            vo = null;
            isNeedAlert = false;
            e = arg1;
            if (this._shiftItem.itemData && this._targetItem.itemData) 
            {
                onBtnClickHandler = (arg1: int, arg2: Boolean): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        Alert.resetToDefault();
                        Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponShift, {"vo": vo}));
                    }
                    if (arg2) 
                    {
                        PulseSharedObject.save("TransferBind", true);
                    }
                    return;
                }
                vo = new ShiftVo();
                vo.srcPosType = this._shiftItem.itemData.posType;
                vo.desPosType = this._targetItem.itemData.posType;
                vo.amuletUid = this._shiftItem.itemData.uid;
                vo.shiftAmuletUid = this._targetItem.itemData.uid;
                vo.materials = [];
                if (this._targetItem.itemData.itemExInfo && !(this._targetItem.itemData.itemExInfo.bind == EBind._EBindYes)) 
                {
                    isNeedAlert = !PulseSharedObject.isTodayNotTips("TransferBind");
                    if (isNeedAlert) 
                    {
                        Alert.alertWinRenderer = CheckBoxWin;
                        Alert.show(Language.getString(27681), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                        return;
                    }
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponShift, {"vo": vo}));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(27682));
            }
            return;
        }

        /* internal  */set shiftItemProperty(arg1: ItemData)
        {
            var loc1=null;
            if (arg1) 
            {
                loc1 = MagicWeaponUtil.nextLvProperty(arg1, this._shiftItem.itemData.color, this._shiftItem.itemData.magicWeaponLv);
                if (loc1) 
                {
                    this._shiftItemList.dataProvider = new DataProvider(loc1);
                    this._shiftItemList.drawNow();
                }
            }
            return;
        }

        /* internal  */set targetProperty(arg1: ItemData)
        {
            var loc1=null;
            if (arg1) 
            {
                loc1 = MagicWeaponUtil.nextLvProperty(arg1, this._shiftItem.itemData.color, this._shiftItem.itemData.magicWeaponLv);
                if (loc1) 
                {
                    this._targetItemList.dataProvider = new DataProvider(loc1);
                    this._targetItemList.drawNow();
                }
            }
            return;
        }

        /* internal  */sortData(arg1: MagicWeaponItemData, arg2: MagicWeaponItemData): int
        {
            if (arg1.itemData.magicWeaponLv >= arg2.itemData.magicWeaponLv) 
            {
                if (arg1.itemData.color >= arg2.itemData.color) 
                {
                    if (arg1.itemData.magicWeaponScore > arg2.itemData.magicWeaponScore) 
                    {
                        return -1;
                    }
                    if (arg1.itemData.magicWeaponScore == arg2.itemData.magicWeaponScore) 
                    {
                        if (arg1.itemData.color > arg2.itemData.color) 
                        {
                            return -1;
                        }
                        if (arg1.itemData.color == arg2.itemData.color && arg1.itemData.magicWeaponLv > arg2.itemData.magicWeaponLv) 
                        {
                            return -1;
                        }
                        return 0;
                    }
                    if (arg1.itemData.color > arg2.itemData.color) 
                    {
                        return -1;
                    }
                    if (arg1.itemData.color == arg2.itemData.color && arg1.itemData.magicWeaponLv > arg2.itemData.magicWeaponLv) 
                    {
                        return -1;
                    }
                    return 1;
                }
                if (arg1.itemData.magicWeaponLv > arg2.itemData.magicWeaponLv) 
                {
                    return -1;
                }
                return 1;
            }
            return 1;
        }

        public /* override */ updateWeaponInfo(): void
        {
            var loc1=null;
            var loc4=null;
            var loc2=Cache.instance.magicWeapon.hasIdentifyAndWear;
            var loc3=Cache.instance.magicWeapon.hasIdentifyAndNoWear;
            loc2.sort(this.sortData);
            loc3.sort(this.sortData);
            loc1 = loc2.concat(loc3);
            var loc5=0;
            var loc6=loc1;
            for(loc4 of loc6) 
            {
                if (!(loc4.itemData.uid == this.getUId(this._shiftItem) || loc4.itemData.uid == this.getUId(this._targetItem))) 
                {
                    continue;
                }
                loc4.isSelect = true;
            }
            if (_materialList && loc1) 
            {
                _materialList.dataProvider = new DataProvider(loc1);
                _materialList.drawNow();
                _materialData = loc1;
                if (pageSelecter) 
                {
                    pageSelecter.maxPage = int(Math.ceil(loc1.length / 6));
                    currentPage();
                }
            }
            return;
        }

        public shiftSucceed(): void
        {
            this.onPutDownShiftItem();
            this.onPutDownTargetItem();
            this.setSelect();
            return;
        }

        protected /* override */ onSelectItemHandler(arg1: ListEvent): void
        {
            var loc1=arg1.item as MagicWeaponItemData;
            if (loc1) 
            {
                if (this._shiftItem && !this._shiftItem.itemData) 
                {
                    if (loc1.itemData.magicWeaponLv < 1) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27697));
                        return;
                    }
                    if (this._targetItem.itemData) 
                    {
                        if (this._targetItem.itemData.uid == loc1.itemData.uid) 
                        {
                            return;
                        }
                        if (this._targetItem.itemData.type != loc1.itemData.type) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(27698));
                            return;
                        }
                        if (this._targetItem.itemData.magicWeaponLv >= loc1.itemData.magicWeaponLv) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(27699));
                            return;
                        }
                        if (loc1.itemData.magicWeaponLv > MagicWeaponConfig.instance.getMaxLvByTypeColor(4, this._targetItem.itemData.color)) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(27700));
                            return;
                        }
                    }
                    this._shiftItem.data = loc1;
                    this.shiftItemProperty = loc1.itemData;
                }
                else if (this._targetItem && !this._targetItem.itemData) 
                {
                    if (this._shiftItem.itemData.uid == loc1.itemData.uid) 
                    {
                        return;
                    }
                    if (this._shiftItem.itemData.type != loc1.itemData.type) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27698));
                        return;
                    }
                    if (this._shiftItem.itemData.magicWeaponLv <= loc1.itemData.magicWeaponLv) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27699));
                        return;
                    }
                    if (this._shiftItem.itemData.magicWeaponLv > MagicWeaponConfig.instance.getMaxLvByTypeColor(4, loc1.itemData.color)) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27700));
                        return;
                    }
                    this._targetItem.data = loc1;
                    this.targetProperty = loc1.itemData;
                }
                this.setSelect();
            }
            return;
        }

        /* internal */ /* var */_bigArrow: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_transferBtn: GButton;

        /* internal */ /* var */_shiftItem: ResolveItemRenderer;

        /* internal */ /* var */_targetItem: ResolveItemRenderer;

        /* internal */ /* var */_shiftItemList: GTileList;

        /* internal */ /* var */_targetItemList: GTileList;
    }
